package com.cn.jinl.service;


import com.cn.jinl.api.*;
import com.cn.jinl.dao.DeviceMapper;
import com.cn.jinl.dao.LineOtherMapper;
import com.cn.jinl.dao.OnDutyProductMapper;
import com.cn.jinl.domain.*;
import com.cn.jinl.utils.*;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class OnDutyProductServiceImpl implements IOnDutyProductService {

    @Autowired
    private IOnDutyPersonService onDutyPersonService;

    @Autowired
    private OnDutyProductMapper onDutyProductMapper;

    @Autowired
    private IProductStorageRelateService productStorageRelateService;

    @Autowired
    private IDeviceService deviceService;

    @Autowired
    private DeviceMapper deviceMapper;

    @Autowired
    private IPlanManageService planManageService;

    @Autowired
    private IOrderService orderService;

    @Autowired
    private IChenxingMachineService chenxingMachineService;

    @Autowired
    private LineOtherMapper lineOtherMapper;

    Object lock = new Object();

    @Override
    public List<OnDutyProduct> getFirstOnDutyProducts() throws Exception {
        Map<String, String> dutyMap = CommonUtil.getOnDutyAndWhiteDay();
        String dutyDay = dutyMap.get("dutyDay");
        String whiteDay = dutyMap.get("whiteDay");
        QueryForm queryForm = new QueryForm();
        queryForm.setWhiteDay(whiteDay);
        queryForm.setDutyDay(dutyDay);
        Map<String, List<OnDutyProduct>> procMap = this.getOnDutyProductList(queryForm);
        List<OnDutyProduct> results = Lists.newLinkedList();
        for(String key:procMap.keySet()){
            List<OnDutyProduct> products = procMap.get(key);
            Iterator<OnDutyProduct> iterator = products.iterator();
            Map<String,String> planIdMaps = Maps.newHashMap();
            while(iterator.hasNext()){
                OnDutyProduct onDutyProduct = iterator.next();
                if(StringUtils.isEmpty(onDutyProduct.getPlanId())){
                    iterator.remove();
                    continue;
                }
                if(planIdMaps.containsKey(onDutyProduct.getPlanId())){
                    String code = planIdMaps.get(onDutyProduct.getPlanId());
                    planIdMaps.put(onDutyProduct.getPlanId(),code+","+onDutyProduct.getDeviceCode());
                    iterator.remove();
                }else{
                    planIdMaps.put(onDutyProduct.getPlanId(),onDutyProduct.getDeviceCode());
                }
            }
            for(OnDutyProduct product:products){
                ProductStorageRelateExample example = new ProductStorageRelateExample();
                example.createCriteria().andDutyDayEqualTo(dutyDay).andTaskIdEqualTo(product.getId());
                Integer count=productStorageRelateService.getCount(example);
                product.setCompleteNum(count);
                product.setJiTai(planIdMaps.get(product.getPlanId()));
            }
            results.addAll(products);
        }
        return results;
    }

    @Override
    public List<OnDutyProduct> getOnDutyProductsByDutyDay(QueryForm queryForm) throws Exception {
        String dutyDay = queryForm.getDutyDay();
        if(StringUtils.isEmpty(dutyDay)){
            return Lists.newArrayList();
        }
        OnDutyProductExample example = new OnDutyProductExample();
        example.createCriteria().andStatusEqualTo(Constants.NORMAL_STATUS).andDutyDayEqualTo(dutyDay).andProductStatusNotEqualTo("0");
        List<OnDutyProduct> list = onDutyProductMapper.selectByExample(example);
        return list;
    }

    @Override
    public Map<String, List<OnDutyProduct>> getOnDutyProductList(QueryForm queryForm) throws Exception {

        if (StringUtils.isEmpty(queryForm.getDutyDay()) || StringUtils.isEmpty(queryForm.getWhiteDay())) {
            return Maps.newHashMap();
        }

        Map<String, List<String>> deviceMaps = deviceService.getDeviceByProcName("包装", "");
        OnDutyProductExample example = new OnDutyProductExample();
        example.createCriteria().andStatusEqualTo(Constants.NORMAL_STATUS).andDutyDayEqualTo(queryForm.getDutyDay()).andWhiteDayEqualTo(queryForm.getWhiteDay());
        List<OnDutyProduct> list = onDutyProductMapper.selectByExample(example);
        Map<String, OnDutyProduct> productMap = Maps.newHashMap();
        for (OnDutyProduct onDutyProduct : list) {
            PlanManage planManage = planManageService.getPlanManageById(onDutyProduct.getPlanId());
            if (planManage!=null){
                if (StringUtils.isNotEmpty(planManage.getCompleteNum())){
                    double num = Double.parseDouble(planManage.getCompleteNum());
                    double total = Double.parseDouble(planManage.getPeieceNum());
                    if(total > 0){
                        String percent = NumberUtil.getPoint((num*100 / total),2)+"%";
                        onDutyProduct.setCompletePercent(percent);
                        onDutyProduct.setCompleteNum(Integer.parseInt(planManage.getCompleteNum()));
                    }
                }
            }
            productMap.put(onDutyProduct.getDeviceCode(), onDutyProduct);
            if (StringUtils.isNotEmpty(onDutyProduct.getTotalNum())){
                BigDecimal bigDecimal = new BigDecimal(onDutyProduct.getTotalNum());
                int totalNum = bigDecimal.intValue();
                onDutyProduct.setTotalNum(String.valueOf(totalNum));
            }
        }
        Map<String, List<OnDutyProduct>> procMap = Maps.newLinkedHashMap();
        procMap.put("单支连排", Lists.<OnDutyProduct>newLinkedList());
        procMap.put("多支", Lists.<OnDutyProduct>newLinkedList());
        procMap.put("伸缩", Lists.<OnDutyProduct>newLinkedList());
        procMap.put("U型", Lists.<OnDutyProduct>newLinkedList());
        for (String key : procMap.keySet()) {
            List<String> deviceCodes = deviceMaps.get(key);
            if (CollectionUtils.isEmpty(deviceCodes)) {
                continue;
            }
            for (String deviceCode : deviceCodes) {
                if (productMap.containsKey(deviceCode)) {
                    procMap.get(key).add(productMap.get(deviceCode));
                } else {
                    OnDutyProduct addProc = new OnDutyProduct();
                    addProc.setDeviceCode(deviceCode);
                    procMap.get(key).add(addProc);
                }
            }
        }
        return procMap;
    }

    @Override
    public Map<String, String> getOnDutyProductStatusMap(QueryForm queryForm) throws Exception {
        List<OnDutyProduct> onDutyProducts = onDutyProductMapper.selectOnDutyProductStatus(queryForm);
        int openNum = 0;
        int waitingNum = 0;
        Map<String, String> resultMap = Maps.newHashMap();
        for (OnDutyProduct onDutyProduct : onDutyProducts) {
            if (StringUtils.equals("2", onDutyProduct.getProductStatus())) {
                ++openNum;
            } else if (StringUtils.equals("1", onDutyProduct.getProductStatus())) {
                ++waitingNum;
            }
            resultMap.put(onDutyProduct.getDeviceCode(), onDutyProduct.getGradeValue());
            resultMap.put(onDutyProduct.getDeviceCode()+"#", onDutyProduct.getPlanName());
        }
        resultMap.put("openNum", openNum + "");
        resultMap.put("waitingNum", waitingNum + "");
        return resultMap;
    }

    @Override
    public List<OnDutyProduct> getTodayOnDutyProducts(String dutyDay, String whiteDay) throws Exception {
        if(StringUtils.isEmpty(dutyDay)||StringUtils.isEmpty(whiteDay)){
            return Lists.newArrayList();
        }
        OnDutyProductExample example = new OnDutyProductExample();
        example.createCriteria().andStatusEqualTo(Constants.NORMAL_STATUS).andDutyDayEqualTo(dutyDay).andWhiteDayEqualTo(whiteDay);
        List<OnDutyProduct> list = onDutyProductMapper.selectByExample(example);
        return list;
    }

    @Override
    public List<ProductStorageRelate> getOnDutyProductsByPlanId(String planId) throws Exception {
        List<ProductStorageRelate> relates = productStorageRelateService.getOnDutyStorageHisByPlanId(planId);
        List<ProductStorageRelate> results = Lists.newLinkedList();
        if(CollectionUtils.isNotEmpty(relates)){
            Map<String,ProductStorageRelate> map = Maps.newLinkedHashMap();
            for(ProductStorageRelate relate : relates){
                String key = relate.getWhiteDay() +"-"+relate.getDutyDay();
                double total = Double.parseDouble(relate.getThNumber());
                if(map.containsKey(key)){
                    total = total + map.get(key).getTotalProductCount();
                    map.get(key).setTotalProductCount(total);
                    map.get(key).setStorageInCount(map.get(key).getStorageInCount()+1);
                }else{
                    relate.setTotalProductCount(total);
                    relate.setStorageInCount(1);
                    map.put(key,relate);
                }
            }
            for(String key : map.keySet()){
                results.add(map.get(key));
            }

        }
        return results;
    }

    @Override
    public String addBatchOnDutyProduct(List<OnDutyProduct> onDutyProducts) throws Exception {
        if (CollectionUtils.isEmpty(onDutyProducts)) {
            return "参数异常！";
        }
        int count = 0;
        synchronized (lock) {
            String whiteDay = onDutyProducts.get(0).getWhiteDay();
            String dutyDay = onDutyProducts.get(0).getDutyDay();
            String userName = onDutyProducts.get(0).getUserName();
            String userId = onDutyProducts.get(0).getUserId();
            if (StringUtils.isEmpty(whiteDay) || StringUtils.isEmpty(dutyDay)) {
                return "参数异常！";
            }
            OnDutyProductExample example = new OnDutyProductExample();
            example.createCriteria().andStatusEqualTo(Constants.NORMAL_STATUS).andDutyDayEqualTo(dutyDay).andWhiteDayEqualTo(whiteDay);
            List<OnDutyProduct> list = onDutyProductMapper.selectByExample(example);
            Map<String, OnDutyProduct> existPlans = Maps.newHashMap();
            for (OnDutyProduct onDutyProduct : list) {
                existPlans.put(onDutyProduct.getDeviceCode(), onDutyProduct);
            }
            List<OnDutyProduct> addList = Lists.newArrayList();
            List<OnDutyProduct> updateList = Lists.newArrayList();
            List<String> planIds = Lists.newArrayList();
            for (OnDutyProduct pt : onDutyProducts) {
                String id = pt.getId();
                if(StringUtils.equals("2",pt.getProductStatus())){
                    planIds.add(pt.getPlanId());
                }
                String deviceCode = pt.getDeviceCode();
                if (StringUtils.isEmpty(id) && existPlans.containsKey(deviceCode)) {
                    return "请刷新页面重试！";
                }
                if (StringUtils.isEmpty(id)) {
                    pt.setId(CommonUtil.generateRandomNum("pt-"));
                    addList.add(pt);
                } else {
                    updateList.add(pt);
                }
            }
            String resp = onDutyPersonService.updateOnDutyPersonByDay(userId, userName, dutyDay, whiteDay);
            if (StringUtils.isNotEmpty(resp)) {
                return "更新表异常";
            }
            if (!CollectionUtils.isEmpty(addList)) {
                count = onDutyProductMapper.batchInsertDutyProducts(addList);
                if (count < 0) {
                    return "插入表异常！";
                }
            }
            if (!CollectionUtils.isEmpty(updateList)) {
                count = onDutyProductMapper.batchUpdateOndutyProducts(updateList);
                if (count < 0) {
                    throw new Exception("更新表异常！");
                }
            }
            if(!CollectionUtils.isEmpty(planIds)){
                List<String> orderIds = planManageService.getOrderIdsBtPlanIds(planIds);
                resp = planManageService.updatePlanManageStatus(planIds,"2");
                if (StringUtils.isNotEmpty(resp)) {
                    throw new Exception(resp);
                }
                if(CollectionUtils.isNotEmpty(orderIds)){
                    resp = orderService.updateOrderProductStatusByIds(orderIds);
                    if (StringUtils.isNotEmpty(resp)) {
                        throw new Exception(resp);
                    }
                }
            }
        }
        return CommonUtil.outStr(count);
    }

    @Override
    public String addOnDutyProduct(OnDutyProduct onDutyProduct) throws Exception {
        return null;
    }

    @Override
    public String updateOnDutyProduct(OnDutyProduct onDutyProduct) throws Exception {
        return null;
    }

    @Override
    public Map<String, OnDutyProductBanVo> getOnDutyProductBanList() throws Exception {

        Map<String, String> dutyMap = CommonUtil.getOnDutyAndWhiteDay();
        String dutyDay = dutyMap.get("dutyDay");
        String whiteDay = dutyMap.get("whiteDay");

        String banZhang = onDutyPersonService.getOnDutyPersonByDutyAndWhite(dutyDay, whiteDay);
        QueryForm queryForm = new QueryForm();
        queryForm.setDutyDay(dutyDay);
        queryForm.setWhiteDay(whiteDay);

        Map<String, List<OnDutyProduct>> listMap = getOnDutyProductList(queryForm);
        Map<String, OnDutyProductBanVo> resultMap = Maps.newHashMap();
        for (String key : listMap.keySet()) {
            List<OnDutyProduct> products = listMap.get(key);
            OnDutyProductBanVo vo = new OnDutyProductBanVo();
            vo.setOnDutyProducts(products);
            vo.setWhiteDay(whiteDay);
            vo.setDutyDay(dutyDay);
            vo.setBanZhang(banZhang);
            vo.setDeviceCount(products.size());
            int productCount = 0;
            int waitCount = 0;
            int freeCount = 0;
            for (int i = 0; i < products.size(); i++) {
                String status = products.get(0).getProductStatus();
                if (StringUtils.equals("0", status)) {
                    freeCount++;
                } else if (StringUtils.equals("1", status)) {
                    waitCount++;
                } else {
                    productCount++;
                }
            }
            vo.setDeviceProdCount(productCount);
            vo.setDeviceWaitCount(waitCount);
            vo.setDeviceFreeCount(freeCount);
            resultMap.put(key, vo);
        }
//        OnDutyProductBanVo onDutyProductBanD = resultMap.get("多支");
//        OnDutyProductBanVo onDutyProductBanL = resultMap.get("单支连排");
//        onDutyProductBanL.getOnDutyProducts().addAll(onDutyProductBanD.getOnDutyProducts());
//        onDutyProductBanL.setDeviceCount(onDutyProductBanD.getDeviceCount() + onDutyProductBanL.getDeviceCount());
//        onDutyProductBanL.setDeviceProdCount(onDutyProductBanD.getDeviceProdCount() + onDutyProductBanL.getDeviceProdCount());
//        onDutyProductBanL.setDeviceWaitCount(onDutyProductBanD.getDeviceWaitCount() + onDutyProductBanL.getDeviceWaitCount());
//        onDutyProductBanL.setDeviceFreeCount(onDutyProductBanD.getDeviceFreeCount() + onDutyProductBanL.getDeviceFreeCount());
        return resultMap;
    }

    @Override
    public Map<String, OnDutyProductBanVo> getOnDutyProductPersonList() throws Exception {

        Map<String,String>dutyMap = CommonUtil.getOnDutyAndWhiteDay();
        String dutyDay = dutyMap.get("dutyDay");
        String whiteDay = dutyMap.get("whiteDay");

        Map<String, OnDutyProductBanVo> resultMap = Maps.newLinkedHashMap();
        String key1 = dutyDay + " " + "白班";
        String key2 = dutyDay + " " + "夜班";

        OnDutyProductBanVo baiban = this.getOnDutyProductBanVo(dutyDay, "1");
        OnDutyProductBanVo yeban = this.getOnDutyProductBanVo(dutyDay, "2");
        if(StringUtils.equals("1",whiteDay)){
            baiban.setHidden("0");
            yeban.setHidden("1");
        }else {
            baiban.setHidden("1");
            yeban.setHidden("0");
        }
        if(StringUtils.isEmpty(baiban.getLocked())){
            baiban.setLocked("0");
        }
        if(StringUtils.isEmpty(yeban.getLocked())){
            yeban.setLocked("0");
        }
        if (Integer.parseInt(baiban.getLocked()) < 5 &&StringUtils.equals("1",whiteDay)) {
            resultMap.put(key1, baiban);
        }
        if (Integer.parseInt(yeban.getLocked()) < 5 &&StringUtils.equals("2",whiteDay)) {
            resultMap.put(key2, yeban);
        }
        return resultMap;
    }

    private OnDutyProductBanVo getOnDutyProductBanVo(String dutyDay, String whiteDay) throws Exception {
        OnDutyProductBanVo onDutyProductBanVo = new OnDutyProductBanVo();

        String banZhang = onDutyPersonService.getOnDutyPersonByDutyAndWhite(dutyDay, whiteDay);
        String locked = onDutyPersonService.getOnDutyLockedByDutyAndWhite(dutyDay, whiteDay);
        QueryForm queryForm = new QueryForm();
        queryForm.setWhiteDay(whiteDay);
        queryForm.setDutyDay(dutyDay);
        List<OnDutyProduct> list = onDutyProductMapper.selectOnDutyProducts(queryForm);
        if(CollectionUtils.isNotEmpty(list)){
            BubbleSort.sortProduct(list);
        }
        DeviceExample deviceExample = new DeviceExample();
        deviceExample.createCriteria().andStatusEqualTo(Constants.NORMAL_STATUS);
        List<Device> devices = deviceMapper.selectByExample(deviceExample);
        Map<String,String>deviceMap = Maps.newHashMap();
        for(Device device :devices){
            deviceMap.put(device.getDeviceCode(),device.getProcessCategory());
        }

        Map<String, List<OnDutyProduct>> prodMap = Maps.newLinkedHashMap();
        for (OnDutyProduct onDutyProduct : list) {
            if(StringUtils.isEmpty(onDutyProduct.getItemTypeId())){
                onDutyProduct.setItemTypeId(deviceMap.get(onDutyProduct.getDeviceCode()));
            }
            String key = onDutyProduct.getItemTypeId() + "_" + onDutyProduct.getUserName();
            String thNumber = onDutyProduct.getThNumber();
            int taskCount = onDutyProduct.getTaskCount();
            if(StringUtils.isNotEmpty(thNumber)){
                double totalNum = Double.parseDouble(thNumber)*taskCount;
                onDutyProduct.setTotalNum(NumberUtil.getPoint(totalNum,2));
            }
            if (prodMap.containsKey(key)) {
                prodMap.get(key).add(onDutyProduct);
            } else {
                List<OnDutyProduct> prods = Lists.newArrayList();
                prods.add(onDutyProduct);
                prodMap.put(key, prods);
            }
        }
        onDutyProductBanVo.setBanZhang(banZhang);
        onDutyProductBanVo.setWhiteDay(whiteDay);
        onDutyProductBanVo.setDutyDay(dutyDay);
        onDutyProductBanVo.setOnDutyPersons(prodMap);
        onDutyProductBanVo.setLocked(locked);
        return onDutyProductBanVo;
    }

    @Override
    public OnDutyProduct getScanOnDutyProductByDeviceCode(String deviceCode) throws Exception {
        if (StringUtils.isEmpty(deviceCode)) {
            return null;
        }

        OnDutyProductExample example = new OnDutyProductExample();
        Map<String, String> dutyMap = CommonUtil.getOnDutyAndWhiteDay();
        String dutyDay = dutyMap.get("dutyDay");
        String whiteDay = dutyMap.get("whiteDay");
        example.createCriteria().andStatusEqualTo(Constants.NORMAL_STATUS).andDeviceCodeEqualTo(deviceCode).andWhiteDayEqualTo(whiteDay).andDutyDayEqualTo(dutyDay);

        List<OnDutyProduct> products = onDutyProductMapper.selectByExample(example);
        if (CollectionUtils.isEmpty(products)) {
            return null;
        }
        return products.get(0);
    }



    @Override
    public Map<String, Object> getOnDutyProductDetail() throws Exception {
        Map<String, String> dutyMap = CommonUtil.getOnDutyAndWhiteDay();
        String dutyDay = dutyMap.get("dutyDay");
        String whiteDay = dutyMap.get("whiteDay");
        String banZhang = onDutyPersonService.getOnDutyPersonByDutyAndWhite(dutyDay, whiteDay);
        OnDutyProductExample example = new OnDutyProductExample();
        example.createCriteria().andStatusEqualTo(Constants.NORMAL_STATUS).andWhiteDayEqualTo(whiteDay).andDutyDayEqualTo(dutyDay);

        List<OnDutyProduct> products = onDutyProductMapper.selectByExample(example);
        Map<String,Object> result = Maps.newHashMap();
        int single = 0;
        int doubleP = 0;
        int scale = 0;
        int U = 0;

        Map<String,List<String>> baoDeviceMaps = deviceService.getDeviceByProcName("包装","");
        for(OnDutyProduct onDutyProduct : products){
            String itemType = onDutyProduct.getItemTypeId();
            if(StringUtils.equals(itemType,"单支连排")){
               if(StringUtils.equals("2",onDutyProduct.getProductStatus())){
                   ++single;
               }
            }else if(StringUtils.equals(itemType,"多支")){
                if(StringUtils.equals("2",onDutyProduct.getProductStatus())){
                    ++doubleP;
                }
            }else if(StringUtils.equals(itemType,"伸缩")){
                if(StringUtils.equals("2",onDutyProduct.getProductStatus())){
                    ++scale;
                }
            }else if(StringUtils.equals(itemType,"U型")){
                if(StringUtils.equals("2",onDutyProduct.getProductStatus())){
                    ++U;
                }
            }
        }

        Map<String,Object> machineMap = chenxingMachineService.getChenxingMachineMaps();
        int totalNum = (int)machineMap.get("totalNum");
        int openNum = (int)machineMap.get("openNum");


        Map<String,List<String>> deviceMaps = deviceService.getDeviceByProcName("挤塑直切","");
        int jsUTotal = deviceMaps.get("U直").size();
        int jsPTotal = deviceMaps.get("普直").size();
        int jsSTotal = deviceMaps.get("伸直").size();

        QueryForm queryForm = new QueryForm();
        queryForm.setDutyDay(dutyDay);
        queryForm.setWhiteDay(whiteDay);
        List<LineOther> lineOthers = lineOtherMapper.selectLineOutList(queryForm);
        int jsU = 0;
        int jsP = 0;
        int jsS = 0;
        for(LineOther lineOther:lineOthers){
            String lineType = lineOther.getLineType();
            if(StringUtils.equals("1",lineType)){
                ++jsU;
            }else if(StringUtils.equals("2",lineType)||StringUtils.equals("3",lineType)){
                ++jsS;
            }else if(StringUtils.equals("4",lineType)){
                ++jsP;
            }
        }
        int tsingle = baoDeviceMaps.get("单支连排").size();
        int tdoubleP = baoDeviceMaps.get("多支").size();
        int tscale = baoDeviceMaps.get("伸缩").size();
        int tU = baoDeviceMaps.get("U型").size();
        result.put("single",single+"/"+tsingle);
        result.put("double",doubleP+"/"+tdoubleP);
        result.put("scale",scale+"/"+tscale);
        result.put("U",U+"/"+tU);

        result.put("jsU",jsU+"/"+jsUTotal);
        result.put("jsP",jsP+"/"+jsPTotal);
        result.put("jsS",jsS+"/"+jsSTotal);
        result.put("Ushape",openNum+"/"+totalNum);
        result.put("banzhang",banZhang);
        result.put("dutyDay",dutyDay);
        result.put("whiteDay",StringUtils.equals(whiteDay,"1")?"白班":"夜班");

        return result;
    }

    @Override
    public Map<String, Integer> getOnDutyRunningProduct() throws Exception {
        List<ProductStorageRelate> relates = productStorageRelateService.getProductStorageRelateToday();
        Map<String,Integer> maps = Maps.newHashMap();
        maps.put("single",0);
        maps.put("double",0);
        maps.put("scale",0);
        maps.put("U",0);
        for(ProductStorageRelate relate:relates){
            String itemTypeId = relate.getItemTypeId();
            int count = 0;
            if(StringUtils.equals(itemTypeId,"单支连排")){
                count = maps.get("single")+1;
                maps.put("single",count);
            }else if(StringUtils.equals(itemTypeId,"多支")){
                count = maps.get("double")+1;
                maps.put("double",count);
            }else if(StringUtils.equals(itemTypeId,"伸缩")){
                count = maps.get("scale")+1;
                maps.put("scale",count);
            }else if(StringUtils.equals(itemTypeId,"U型")){
                count = maps.get("U")+1;
                maps.put("U",count);
            }
        }
        return maps;
    }

    @Override
    public List<OnDutyProduct> getTodayOnDutyProductTask() throws Exception {
        OnDutyProductExample example = new OnDutyProductExample();
        Map<String, String> dutyMap = CommonUtil.getOnDutyAndWhiteDay();
        String dutyDay = dutyMap.get("dutyDay");
        String whiteDay = dutyMap.get("whiteDay");
        example.createCriteria().andDutyDayEqualTo(dutyDay).andWhiteDayEqualTo(whiteDay).andStatusEqualTo(Constants.NORMAL_STATUS).andProductStatusEqualTo("2");
        example.setOrderByClause("create_time desc");
        List<OnDutyProduct> onDutyProducts = onDutyProductMapper.selectByExample(example);
        return onDutyProducts;
    }

    @Override
    public OnDutyProduct getPlanIdByTaskId(String taskId) throws Exception {
        OnDutyProductExample example = new OnDutyProductExample();
        example.createCriteria().andIdEqualTo(taskId).andStatusEqualTo(Constants.NORMAL_STATUS);
        List<OnDutyProduct> products = onDutyProductMapper.selectByExample(example);
        if(CollectionUtils.isEmpty(products) || products.size() > 1){
            return null;
        }
        return products.get(0);
    }

    @Override
    public Map<String, String> getOnDutyAndWhiteDay() throws Exception {
        Map<String, String> dutyMap = CommonUtil.getOnDutyAndWhiteDay();
        return dutyMap;
    }


}
